
<!DOCTYPE html>
<html>
  <head>
    
<meta charset="utf-8" >

<title>狄泰C++第4课-布尔类型和引用 | Blog</title>
<meta name="description" content="个人生活，学习记录">

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.min.css">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="shortcut icon" href="https://qiaoxu123.github.io/favicon.ico?v=1589072669553">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css">
<link rel="stylesheet" href="https://qiaoxu123.github.io/styles/main.css">



<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>



  </head>
  <body>
    <div id="app" class="main">
      <div class="site-header-container">
  <div class="site-header">
    <div class="left">
      <a href="https://qiaoxu123.github.io">
        <img class="avatar" src="https://qiaoxu123.github.io/images/avatar.png?v=1589072669553" alt="" width="32px" height="32px">
      </a>
      <a href="https://qiaoxu123.github.io">
        <h1 class="site-title">Blog</h1>
      </a>
    </div>
    <div class="right">
      <transition name="fade">
        <i class="icon" :class="{ 'icon-close-outline': menuVisible, 'icon-menu-outline': !menuVisible }" @click="menuVisible = !menuVisible"></i>
      </transition>
    </div>
  </div>
</div>

<transition name="fade">
  <div class="menu-container" style="display: none;" v-show="menuVisible">
    <div class="menu-list">
      
        
          <a href="https://qiaoxu123.github.io" class="menu purple-link">
            首页
          </a>
        
      
        
          <a href="https://qiaoxu123.github.io/archives" class="menu purple-link">
            归档
          </a>
        
      
        
          <a href="https://qiaoxu123.github.io/tags" class="menu purple-link">
            标签
          </a>
        
      
    </div>
  </div>
</transition>


      <div class="content-container">
        <div class="post-detail">
          
          <h2 class="post-title">狄泰C++第4课-布尔类型和引用</h2>
          <div class="post-info post-detail-info">
            <span><i class="icon-calendar-outline"></i> 2019-08-01</span>
            
              <span>
                <i class="icon-pricetags-outline"></i>
                
                  <a href="https://qiaoxu123.github.io/tag/6gpyLRVQ_/">
                    C++编程
                    
                  </a>
                
              </span>
            
          </div>
          <div class="post-content">
            <!-- more -->
<h3 id="一-布尔类型">一、布尔类型</h3>
<p><strong>C++中的布尔类型</strong></p>
<ul>
<li>C++在C语言的<strong>基本类型</strong>系统之上增加了<code>bool</code></li>
<li>C++中的<code>bool</code>可取的值只有<code>true</code>和<code>false</code></li>
<li>理论上<code>bool</code>只占用一个字节</li>
</ul>
<p>注意：<code>true</code>代表<strong>真值</strong>，编译器内部用<strong>1</strong>来表示；<code>false</code>代表<strong>非真值</strong>，编译器内部用<strong>0</strong>来表示</p>
<ul>
<li>
<p>bool类型只有<code>true</code>（非0）和<code>false</code>(0)两个值</p>
</li>
<li>
<p>C++编译器会将非0值转换为<code>true</code>，0值转换为<code>false</code></p>
</li>
</ul>
<p><strong>编程实验：布尔类型的使用</strong></p>
<pre><code class="language-c++">
#include&lt;iostream&gt;

int main(int argc,char *argv[]){
	bool b = false;
	int a = b;
	
	printf(&quot;sizeof(b) = %d\n&quot;,sizeof(b));
	printf(&quot;b = %d,a = %d\n&quot;,b,a);
	
	b = 3;
	a = b;
	
	printf(&quot;b = %d, a = %d\n&quot;, b, a);
    
    b = -5;
    a = b;
    
    printf(&quot;b = %d, a = %d\n&quot;, b, a);
    
    a = 10;
    b = a;
    
    printf(&quot;a = %d, b = %d\n&quot;, a, b);
    
    a = 0;
    b = a;
    
    printf(&quot;a = %d, b = %d\n&quot;, a, b);
    
    return 0;
} 
</code></pre>
<p>打印结果：</p>
<pre><code>
sizeof(b) = 1
b = 0,a = 0
b = 1, a = 1
b = 1, a = 1
a = 10, b = 1
a = 0, b = 0
</code></pre>
<p>布尔类型是C++中的基本数据类型</p>
<ul>
<li>可以定义<code>bool</code>类型的<strong>全局变量</strong></li>
<li>可以定义<code>bool</code>类型的<strong>常量</strong></li>
<li>可以定义<code>bool</code>类型的<strong>指针</strong></li>
<li>可以定义<code>bool</code>类型的<strong>数组</strong></li>
<li>····</li>
</ul>
<hr>
<h3 id="二-三目运算符">二、三目运算符</h3>
<p>C++对<strong>三目运算符</strong>进行了<strong>升级</strong></p>
<pre><code class="language-c++">
int a = 1;
int b = 2;
(a &lt; b? a : b) = 3;
printf(&quot;a = %d,b = %d\n&quot;,a,b);
</code></pre>
<ul>
<li><strong>C语言中</strong>的三目运算符返回的是<strong>变量值</strong>
<ul>
<li><strong>不能</strong>作为<strong>左值</strong>使用</li>
</ul>
</li>
<li><strong>C++<strong>中的三目运算符可直接返回</strong>变量本身</strong>
<ul>
<li>既可作为<strong>右值</strong>使用，又可作为<strong>左值</strong>使用</li>
</ul>
</li>
</ul>
<p><strong>注意：三目运算符可能返回的值中如果有一个常量值，则不能作为左值使用</strong></p>
<hr>
<h3 id="三-c中的引用">三、C++中的引用</h3>
<p><strong>变量名回顾</strong></p>
<ul>
<li><strong>变量</strong>是一段实际连续<strong>存储空间的别名</strong></li>
<li>程序中通过<strong>变量</strong> 来申请并<strong>命名存储空间</strong></li>
<li>通过<strong>变量</strong>的名字可以<strong>使用存储空间</strong></li>
</ul>
<figure data-type="image" tabindex="1"><img src="https://img-blog.csdn.net/20180405220322291?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3NTEzMjIx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" loading="lazy"></figure>
<ul>
<li>
<p>在C++中新增加了引用的概念</p>
<ul>
<li>引用可以看作一个已定义变量的别名</li>
<li>引用的语法：Type&amp;name = var;</li>
</ul>
<pre><code class="language-c++">  
int a = 4;
int&amp; b = a;	//b 为 a的别名

b = 5;  //操作b就是操作a
</code></pre>
<p><strong>注意</strong>：普通引用在<strong>定义时</strong>必须<strong>用同类型的变量进行初始化</strong></p>
</li>
</ul>
<p><strong>实例分析：引用初体验</strong></p>
<pre><code class="language-c++">
#include&lt;stdio.h&gt;

int main(int argc,char *argv[]){
	int a = 4;
	int&amp; b = a;
	
	b = 5;
	
	printf(&quot;a = %d\n&quot;,a);
	printf(&quot;b = %d\n&quot;,b);
	printf(&quot;&amp;a = %p\n&quot;,&amp;a);
	printf(&quot;&amp;b = %p\n&quot;,&amp;b);
	
	return 0;
}
</code></pre>
<p>打印结果：</p>
<pre><code>
a = 5
b = 5
&amp;a = 000000000062FE44
&amp;b = 000000000062FE44
</code></pre>
<p><strong>C++对三目运算符都做了什么？</strong></p>
<ul>
<li>当三目运算符的<strong>可能返回都是变量时</strong>，<strong>返回的是变量引用</strong></li>
<li>当三目运算符额<strong>可能返回中有常量时</strong>，<strong>返回的是值</strong></li>
</ul>
<pre><code class="language-c++">int a = 1;
int b = 2;

(a &lt; b?a : b) = 3;	//正确，返回a或b的引用，可作为左值
(a &lt; b?1 : b) = 4;	//错误，返回1或b的值，不能作为左值
</code></pre>
<hr>
<h3 id="四-总结">四、总结</h3>
<ul>
<li><code>bool</code>类型是C++新增加的<strong>基础类型</strong></li>
<li><code>bool</code>类型的值只能是<code>true</code>和<code>false</code></li>
<li>C++中的三目运算符<strong>可作为左值使用</strong></li>
<li>C++中的<strong>引用可以看作变量的别名来使用</strong></li>
<li>三目运算符的<strong>可能返回都是变量时</strong>，<strong>返回的是引用</strong></li>
</ul>

          </div>
        </div>

        
          <div class="next-post">
            <a class="purple-link" href="https://qiaoxu123.github.io/post/3-C++Courses/">
              <h3 class="post-title">
                下一篇：狄泰C++第3课-进化后的const分析
              </h3>
            </a>
          </div>
          
      </div>

      

      <div class="site-footer">
  <div class="slogan">个人生活，学习记录</div>
  <div class="social-container">
    
      
    
      
    
      
    
      
    
      
    
  </div>
  Powered by <a href="https://github.com/getgridea/gridea" target="_blank">Gridea</a> | <a class="rss" href="https://qiaoxu123.github.io/atom.xml" target="_blank">RSS</a>
</div>


    </div>
    <script type="application/javascript">

hljs.initHighlightingOnLoad()

var app = new Vue({
  el: '#app',
  data: {
    menuVisible: false,
  },
})

</script>




  </body>
</html>
